<?php


namespace App\Http\Controllers\Weapp;


use App\Service\Pay\PayFactory;
use App\Tool\Json;
use App\Tool\PayStatus;
use App\Tool\PayType;

class Pay extends Base
{

    function getPayParam()
    {


        try {


            $type = request()->input('pay_type');

            $id = request()->input('id');

            $id_array = explode('*', $id);

            $order_info_array = [];

            //支付总金额
            $total_money=0;

            foreach ($id_array as $key => $value) {

                $order_info = \App\Model\Order::findOrFail($value);


                $status = $order_info->status;

                PayStatus::throwError($status);

                $total_money+=$order_info->real_money;

                $order_info_array[] = $order_info->toArray();



            }

            $pay_type=PayType::getPayType($type);


            $pay = PayFactory::getPay($pay_type);

            $data = $pay->getParam(
                '商品测试',
                $this->createOrderNoForPay($order_info_array),
                $total_money*100,
                \App\Tool\Notify::getOrderNotifyUrlByPayType($pay_type),
                request()->input('openid'),
                json_encode(['id' => $id, 'type' => $type])
            );


            return Json::code(1, 'success', $data);

        } catch (\Exception $exception) {


//            return Json::code();

            return Json::code(2, $exception->getMessage());

        }


    }

    function createOrderNoForPay($order_info_array)
    {

//        uuid();

        $id = join('*', array_column($order_info_array, 'id'));

        return uniqid() . "_" . $id;


    }

}
